package com.github_dididipapa.shortlink.consumer.comsumer.direct;

import com.alibaba.fastjson2.JSON;
import com.github_dididipapa.shortlink.consumer.entity.*;
import com.github_dididipapa.shortlink.consumer.mapper.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Header;
import java.io.IOException;

/**
 * @description: To be defined
 * @author: dididi_papa
 * @email: mitis1624087624@gmail.com
 * @date: 2025/10/17  16:40
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class RouterMonitor {

    private final AccessStatsMapper accessStatsMapper;
    private final LocalStatsMapper localStatsMapper;
    private final OsStatsMapper osStatsMapper;
    private final LinkBrowserStatsMapper linkBrowserStatsMapper;
    private final LinkDeviceStatsMapper linkDeviceStatsMapper;
    private final LinkNetworkStatsMapper linkNetworkStatsMapper;


    @RabbitListener(queuesToDeclare = @Queue(value = "accessStats", durable = "true"), ackMode = "MANUAL")
    public void accessStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理访问统计消息: {}", message);
            AccessStats accessStats = JSON.parseObject(message, AccessStats.class);
            accessStatsMapper.shortLinkAccessStats(accessStats);
            channel.basicAck(deliveryTag, false);
            log.info("访问统计消息处理完成: gid={}", accessStats.getGid());
        } catch (Exception e) {
            log.error("处理访问统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
    
    @RabbitListener(queuesToDeclare = @Queue(value = "localStats", durable = "true"), ackMode = "MANUAL")
    public void localStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理地区统计消息: {}", message);
            LocalStats localStats = JSON.parseObject(message, LocalStats.class);
            localStatsMapper.updateDetil(localStats);
            channel.basicAck(deliveryTag, false);
            log.info("地区统计消息处理完成: gid={}", localStats.getGid());
        } catch (Exception e) {
            log.error("处理地区统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
    
    @RabbitListener(queuesToDeclare = @Queue(value = "osStats", durable = "true"), ackMode = "MANUAL")
    public void osStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理操作系统统计消息: {}", message);
            OsStats osStats = JSON.parseObject(message, OsStats.class);
            osStatsMapper.updateDetil(osStats);
            channel.basicAck(deliveryTag, false);
            log.info("操作系统统计消息处理完成: gid={}", osStats.getGid());
        } catch (Exception e) {
            log.error("处理操作系统统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
    
    @RabbitListener(queuesToDeclare = @Queue(value = "browserStats", durable = "true"), ackMode = "MANUAL")
    public void browserStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理浏览器统计消息: {}", message);
            LinkBrowserStats linkBrowserStats = JSON.parseObject(message, LinkBrowserStats.class);
            linkBrowserStatsMapper.updateDetil(linkBrowserStats);
            channel.basicAck(deliveryTag, false);
            log.info("浏览器统计消息处理完成: gid={}", linkBrowserStats.getGid());
        } catch (Exception e) {
            log.error("处理浏览器统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
    
    @RabbitListener(queuesToDeclare = @Queue(value = "linkDeviceStats", durable = "true"), ackMode = "MANUAL")
    public void linkDeviceStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理设备统计消息: {}", message);
            LinkDeviceStats linkDeviceStats = JSON.parseObject(message, LinkDeviceStats.class);
            linkDeviceStatsMapper.shortLinkDeviceState(linkDeviceStats);
            channel.basicAck(deliveryTag, false);
            log.info("设备统计消息处理完成: gid={}", linkDeviceStats.getGid());
        } catch (Exception e) {
            log.error("处理设备统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
    
    @RabbitListener(queuesToDeclare = @Queue(value = "linkNetworkStats", durable = "true"), ackMode = "MANUAL")
    public void linkNetworkStats(String message, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long deliveryTag) {
        try {
            log.info("开始处理网络统计消息: {}", message);
            LinkNetworkStats linkNetworkStats = JSON.parseObject(message, LinkNetworkStats.class);
            linkNetworkStatsMapper.shortLinkNetworkState(linkNetworkStats);
            channel.basicAck(deliveryTag, false);
            log.info("网络统计消息处理完成: gid={}", linkNetworkStats.getGid());
        } catch (Exception e) {
            log.error("处理网络统计消息失败: message={}", message, e);
            try {
                channel.basicNack(deliveryTag, false, true);
            } catch (IOException ioException) {
                log.error("消息拒绝失败", ioException);
            }
            throw new RuntimeException("消息处理失败", e);
        }
    }
}